import { ipcMain, BrowserWindow } from 'electron'
import { optimizer } from '@electron-toolkit/utils'
import ipcEventChannels from '../../common/ipcEventChannels'

export default () => {
    ipcMain.handle(ipcEventChannels.WIN_IS_MAXIMIZED, (event) => {
        return BrowserWindow.fromWebContents(event.sender).isMaximized()
    })

    ipcMain.on(ipcEventChannels.WIN_LISTEN_MAXIMIZE, (event) => {
        BrowserWindow.fromWebContents(event.sender).on('maximize', () => {
            event.sender.send(ipcEventChannels.WIN_LISTEN_MAXIMIZE_CALLBACK)
        })
    })

    ipcMain.on(ipcEventChannels.WIN_LISTEN_UNMAXIMIZE, (event) => {
        BrowserWindow.fromWebContents(event.sender).on('unmaximize', () => {
            event.sender.send(ipcEventChannels.WIN_LISTEN_UNMAXIMIZE_CALLBACK)
        })
    })

    ipcMain.on(ipcEventChannels.WIN_LISTEN_MINIMIZE, (event) => {
        BrowserWindow.fromWebContents(event.sender).on('minimize', () => {
            event.sender.send(ipcEventChannels.WIN_LISTEN_MINIMIZE_CALLBACK)
        })
    })

    // 注册控制窗口的ipc channel
    optimizer.registerFramelessWindowIpc()
}
